let table, $, layer;
let selectedCompany;
var id = RequestURLParam.getParam("id");


layui.use(['table', 'jquery', 'layer'], function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;

    $('#createBtn').bind('click', createCompany);

    // 1)取得localstorage的sza_localstorage_data变量
    if (localStorage.sza_localstorage_data) {
        let comName = localStorage.sza_localstorage_data;
        comName = JSON.parse(comName);
        localStorage.removeItem("sza_localstorage_data");

        $('#name').html(comName.name);
    }

    //第一个实例
    table.render({
        id: 'demo'
        , elem: '#demo'
        , height: 350
        , url: '/company-business-licence/list' //数据接口
        , cols: [[
            {type: 'radio'}
            , {field: 'name', title: '公司名称', sort: true}
            , {field: 'creditCode', title: '信用代码'}
        ]]
        , page: true
        //单选按钮反选
        // ,parseData: function(res){
        //     if(id != "" && id != undefined){
        //         console.log(res);
        //
        //         res.data.forEach(function (item, index) {
        //             if(item.id == id){
        //                 res.data[index].LAY_CHECKED = true;
        //                 res.data[index].companyname=true;
        //             }
        //         })
        //
        //     }
        //
        //     return res;
        // }
    });
    //单选按钮一行数据绑定lable
    table.on('radio(demo)', function (obj) {
        selectedCompany = obj.data;
        if (obj.checked) {
            $('#name').html(`${selectedCompany.name}`);
        }
    });
});

//点击确定事件
function save() {
    if (!selectedCompany) {
        layer.alert("请选择公司后确定！");
        return false;
    }

    localStorage.sza_localstorage_data = JSON.stringify(selectedCompany);
    parent.layer.closeAll();
}

function del() {
    parent.layer.closeAll();
}

function select() {
    let name = $('input[name=name]').val(); //取公司名
    let code = $('input[name=creditCode]').val(); //取信用代码

    $.ajax({
        url: '/company-business-licence/select',
        type: 'post',
        data: {
            'name': name,
            'creditCode': code
        },
        success: function (res) {
            table.render({
                elem: '#demo',
                height: 350,
                data: res.data,
                cols: [[
                    {type: 'radio'}
                    , {field: 'name', title: '公司名称', sort: true}
                    , {field: 'creditCode', title: '信用代码'}
                ]]
                , page: true
            });
        }
    });
}

/*
* 创建公司
* */
function createCompany() {
    layer.open({
        type: 2
        , title: '添加机构'
        , area: ['1105px', '700px']
        , maxmin: false
        , scrollbar: false
        , content: ['seekCompany.html', 'no']
        , zIndex: layer.zIndex
        , end: function () {

        }
    });
}